from django.db import models
from django.contrib.auth.models import AbstractUser
import os

def user_avatar_path(instance, filename):
    """
    自定义头像上传路径
    格式: /media/avatar/user-{user_id}-{filename}
    """
    # 获取文件扩展名
    ext = filename.split('.')[-1]
    # 构建新的文件名
    new_filename = f"user-{instance.id}-{filename}"
    # 返回完整路径
    return os.path.join('avatar', new_filename)

# Create your models here.
class User(AbstractUser):
    phone = models.CharField(verbose_name='手机号', max_length=24, blank=True, null=True, default=None)
    uid = models.CharField(verbose_name='UID', max_length=255, blank=True, null=True, default='')
    avatar = models.ImageField(verbose_name='头像', upload_to=user_avatar_path, blank=True, null=True)
    is_superuser = models.BooleanField(verbose_name='是否为超级管理员', default=False)

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return self.username


class TodoList(models.Model):
    description = models.TextField()
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='创建人',
        related_name='todo_list',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '待办事项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title